package com.example.shopping_web_back.controller;

import com.example.shopping_web_back.pojo.ReceivePlace;
import com.example.shopping_web_back.pojo.Result;
import com.example.shopping_web_back.service.ReceivePlaceService;
import com.example.shopping_web_back.utils.ThreadLocalUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ReceivePlace")
@Validated
public class ReceivePlaceController {

    @Autowired
    private ReceivePlaceService receivePlaceService;

    @GetMapping("/searchReceivePlace")
    public Result<List<ReceivePlace>> searchReceivePlace(@RequestHeader(name = "Customer")String token) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String customerName = (String)map.get("customer_name");
        List<ReceivePlace> pbReceivePlace = receivePlaceService.searchReceivePlace(customerName);
        return Result.success(pbReceivePlace);
    }

    @PostMapping("/addReceivePlace")
    public Result addReceivePlace(@RequestBody ReceivePlace receivePlace) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String customerName = (String)map.get("customer_name");
        receivePlace.setCustomerName(customerName);
        ReceivePlace result = receivePlaceService.findReceivePlace(receivePlace);
        if(result == null) {
            receivePlaceService.addReceivePlace(receivePlace);
            return Result.success();
        }
        else return Result.error("该收货地址您已经添加过了，请勿重复添加!");
    }

    @Data
    // 定义一个新的对象，主要用于存储两个收货地址以便更新
    public static class UpdateReceivePlaceRequest {
        private ReceivePlace oldReceivePlace;
        private ReceivePlace newReceivePlace;
    }

    @PostMapping("/updateReceivePlace")
    public Result updateReceivePlace(@RequestBody UpdateReceivePlaceRequest list) {
        ReceivePlace old_receivePlace = list.getOldReceivePlace();
        ReceivePlace new_receivePlace = list.getNewReceivePlace();
        Map<String, Object> map = ThreadLocalUtil.get();
        String customerName = (String)map.get("customer_name");
        old_receivePlace.setCustomerName(customerName);
        new_receivePlace.setCustomerName(customerName);
        ReceivePlace findReceivePlace = receivePlaceService.findReceivePlace(new_receivePlace);
        if (findReceivePlace != null) {
            return Result.error("新添加的收货地址已经存在了，请勿重复添加!");
        }
        else {
            findReceivePlace = receivePlaceService.findReceivePlace(old_receivePlace);
            receivePlaceService.updateReceivePlace(findReceivePlace.getId(), new_receivePlace);
            return Result.success();
        }
    }

    @PostMapping("/deleteReceivePlace")
    public Result deleteReceivePlace(@RequestBody ReceivePlace receivePlace) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String customerName = (String)map.get("customer_name");
        receivePlace.setCustomerName(customerName);
        receivePlaceService.deleteReceivePlace(receivePlace);
        return Result.success();
    }
}
